package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.util.*;

/**
 * Created by niufei on 2017/3/30.
 *
 */
@Data
public class QuarantineCenterHarmless implements Serializable{
    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = -3609934194187976330L;

    /**
     * 主键id
     */
    private Long id;

    /**
     * 无害化单据编号
     */
    private String number;

    /**
     * 养殖场id
     */
    private Long breedFactoryId;

    /**
     * 养殖场name
     */
    private String breedFactoryName;
    /**
     * 养殖产品种类
     */
    private Integer productType;

    /**
     * 单位
     */
    private Integer unit;

    /**
     * 数量
     */
    private Integer quantity;
    /**
     * 操作人
     */
    private String operator;

    /**
     * 处理方式
     */
    private Integer treatment;

    /**
     * 处理日期
     */
    private Date date;

    /**
     * 养殖区域
     */
    private Integer area;

    /**
     * 无害化照片
     */
    @JsonIgnore
    private String photosJson;
    /**
     * 无害化照片---------不存入数据库
     */
    private Map<String,Map<String,String>>photos;
    /**
     * 无害化video
     */
    private String videoJson;
    /**
     * 无害化video--------不存入数据库
     */
    private Map<String,String> video;
    /**
     * 视频快照
     */
    private String videoSnapShootJson;
    /**
     * 视频快照--------不存入数据库
     */
    private Map<String,Map<String,String>> videoSnap;

    /**
     * 栏舍号id
     */
    private Long stableId;

    /**
     * 栏舍号
     */
    private String stableNumber;

    /**
     * 死亡原因
     */
    private Integer deathReason;

    /**
     * 耳标号 存数据库
     */
    @JsonIgnore
    private String earNumberJson;

    /**
     * 耳标号 不存数据库
     */
    private List<String> earNumbers;
    /**
     * 备注
     */
    private String remark;

    /**
     * 状态，0：未提交，1：已生效
     */
    private Integer status;
    /**
     *'1：正常，-1：删除'
     */
    private Integer deleteFlag;
    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;

    public void setPhotosJson(String photosJson)throws Exception{
        this.photosJson=photosJson;
        if(Strings.isNullOrEmpty(photosJson)){
            this.photos= Maps.newHashMap();
        }else {
            this.photos = objectMapper.readValue(photosJson, new TypeReference<Map<String,Map<String,String>>>(){});
        }
    }
    public void setPhotos(Map<String,Map<String,String>> photos){
        this.photos=photos;
        if(photos==null ||photos.isEmpty()){
            this.photosJson=null;
        }else{
            try{
                this.photosJson = objectMapper.writeValueAsString(photos);
            }catch (Exception e){

            }
        }
    }
    public void setVideoJson(String videoJson)throws Exception{
        this.videoJson=videoJson;
        if(Strings.isNullOrEmpty(videoJson)){
            this.video= Collections.emptyMap();
        } else{
            this.video = objectMapper.readValue(videoJson,new TypeReference<Map<String,String>>() {});
        }
    }
    public void setVideo(Map<String,String> video) {
        this.video = video;
        if(video ==null ||video.isEmpty()){
            this.videoJson = null;
        }else{
            try {
                this.videoJson = objectMapper.writeValueAsString(video);
            } catch (Exception e) {
            }
        }
    }

    public void setVideoSnapShootJson(String videoSnapShootJson) throws Exception{
        this.videoSnapShootJson = videoSnapShootJson;
        if(Strings.isNullOrEmpty(videoSnapShootJson)){
            this.videoSnap= Maps.newHashMap();
        } else{
            this.videoSnap= objectMapper.readValue(videoSnapShootJson,new TypeReference<Map<String,Map<String,String>>>() {});
        }
    }

    public void setVideoSnap(Map<String,Map<String,String>> videoSnap) {
        this.videoSnap = videoSnap;
        if(videoSnap ==null ||videoSnap.isEmpty()){
            this.videoSnapShootJson = null;
        }else{
            try {
                this.videoSnapShootJson = objectMapper.writeValueAsString(videoSnap);
            } catch (Exception e) {
            }
        }
    }


    public void setEarNumberJson(String earNumberJson){
        this.earNumberJson = earNumberJson;

        if(org.assertj.core.util.Strings.isNullOrEmpty(earNumberJson)){
            this.earNumbers = new ArrayList<>();
        }else {
            try {
                this.earNumbers =
                        objectMapper.readValue(earNumberJson, new TypeReference<List<String>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }


    public void setEarNumbers(List<String> earNumbers){
        this.earNumbers = earNumbers;

        if(earNumbers == null || earNumbers.isEmpty()){
            this.earNumberJson = null;
        }else {
            try {
                this.earNumberJson =
                        objectMapper.writeValueAsString(earNumbers);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }

}
